package com.xlj.springbootmybatis.user.service;

import com.github.pagehelper.PageHelper;
import com.xlj.springbootmybatis.user.entity.User;
import com.xlj.springbootmybatis.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xlj
 * @Description:springboot整合mybatis demo
 * @Date: Created in 14:07 2019\2\1 0001
 */
@Service
public class Userservice {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 感觉用户id查询用户
     * @param userId
     * @return
     */
    public User getUserById(Integer userId){
        return userMapper.getUserById(userId);
    }

    /**
     * Springboot整合JDBC
     * @param userName
     * @param password
     * @param age
     */
    public int saveUser(String userName, String password, Integer age) {
        return jdbcTemplate.update("insert into user(user_name, password, age) value (?, ?, ?)", userName, password, age);
    }

    /**
     * 新建用户
     * @param user
     * @return
     */
    public int addUser(User user){
        return userMapper.addUser(user);
    }

    /**
     * 根据用户id删除用户
     * @param userId
     * @return
     */
    public int delUserByUserId(Integer userId){
        return userMapper.delUserByUserId(userId);
    }

    /**
     * 根据用户名模糊查询用户
     * @param userName
     * @return
     */
    public List<User> getUserByUserName(String userName){
        return userMapper.getUserByUserName(userName);
    }

    /**
     * 用户列表
     */
    public List<User> getUserList(int pageNum, int pageSize, User user){
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.getUserList(user);
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    public int updateUserByUserId(User user){
        return userMapper.updateUserByUserId(user);
    }
}
